<%--
  Created by IntelliJ IDEA.
  User: Dhy
  Date: 2023/9/5
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加学生信息</title>
</head>
<body>
<%--通过form表单进行添加--%>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
<%--            lay-verify="required"--%>
            <input type="text" name="student_name"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="student_password"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="text" name="student_sex"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="student_age"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
<%--    通过下拉框的形式选择教师数据--%>
    <div class="layui-form-item">
        <label class="layui-form-label">教师</label>
        <div class="layui-col-md4">
            <div class="layui-input-inline">
                <select  name="teacher_id"></select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">照片</label>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                <i class="layui-icon layui-icon-upload"></i>图片上传
            </button>
            <div style="width: 132px;">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;">
                    <div id="ID-upload-demo-text"></div>
                </div>
                <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
                    <div class="layui-progress-bar" lay-percent=""></div>
                </div>
                <%--           建一个隐藏的input--%>
                <input type="hidden" name="photo">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="menuAdd">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="../layui/layui.js"></script>
<script>
    layui.use(['form', 'laydate', 'util','element'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        $ = layui.$;    //全局变量
        var upload = layui.upload;
        var element = layui.element;

        // 单图片上传
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            url: '${pageContext.request.contextPath}/upload', // 此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });

                element.progress('filter-demo', '0%'); // 进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function(res){
                // 若上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                //属性选择器
                $('[name="photo"]').val(res.src);
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },
            error: function(){
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
            // 进度条
            progress: function(n, elem, e){
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        // 提交事件
        form.on('submit(menuAdd)', function(data){
            var field = data.field; // 获取表单字段值

            //防止重复点击
            $(".layui-btn").addClass('layui-btn-disabled');
            $(".layui-btn").attr('disabled','disabled');

            // 此处可执行 Ajax 等操作
            $.ajax({
                url: '${pageContext.request.contextPath}/student?method=add',   // ajax请求地址 地址/开头是接口，直接写是页面
                data: field,  // 请求时传递的参数
                type: 'post',  // 请求的方式，get\post
                dataType: 'json', // 、json
                success: function(result){  // 请求成功时的回调， result请求返回的数据。
                    console.log(result);
                    if (result.code === 200){
                        layer.msg("添加成功");
                        //添加成功后跳转到list界面
                        $('#lookposlist').click();
                        $('.layui-body>div').load("${pageContext.request.contextPath}/Student/studentList.jsp");

                        // window.location.href="http://localhost:8080/"
                    }else {
                        layer.msg(result.msg);
                    }
                    //解放提交按钮
                    $(".layui-btn").removeClass('layui-btn-disabled');
                    $(".layui-btn").removeAttr('disabled');

                },
                error: function(result){   // 请求失败时的回调
                    console.log(result);
                    layer.msg(result.msg);
                }
            });

            return false; // 阻止默认 form 跳转
        });

        // 通过下拉框的形式选择教师
        $.ajax({
            url:'${pageContext.request.contextPath}/teacher?method=teacherList',    //定位资源的访问
            type:'get',
            dataType:'json',
            success:function (result) {
                var html = '<option value="">请选择老师</option>';
                if (result.code === 0){
                    // alert(result.res);
                    //循环取数据
                    $.each(result.res,function (index,item) {
                        html += '<option value="'+ item.teacherId+'">'+item.teacherName+'</option>'
                        $('[name="teacher_id"]').html(html);
                        form.render();
                    })
                }
            }
        })

    });
</script>

</body>
</html>

